<template>
  <div class="list">
    <div class="content-head" style="margin-bottom: 20px;">
      <div class="sort-keys">
        <div class="tab-list">
           <div class="item" @click="user_type = item.key" :class="{actived: user_type === item.key}"
             v-for="(item, index) in userTypeList">
             {{item.text}}
           </div>
         </div>

        <a-input placeholder="用户名/手机号/银行卡号" style="width: 180px;margin-right:10px;" v-model="key" allowClear :size="size"
          @input="onSearch" />
        <a-button :size="size" type="primary" @click="refreshData" style="margin-right:10px;">刷新</a-button>
        <!-- <a-select style="width: 150px;margin-right:10px;" v-model="category_id" placeholder="分类" allowClear>
          <a-select-option :value="item.id" v-for="item in category" :key="item.id">{{ item.title }}</a-select-option>
        </a-select> -->

      </div>
      <!-- <a-button type="primary" icon="plus" @click="handleCreate" v-if="!selectMode">创建{{resourceTypeText}}</a-button> -->
      <a-button type="primary" class="add" @click="handleCreate" v-if="user_type==2">
        新增后台提现账户
      </a-button>
    </div>
    <a-table class="table" :rowKey="row => row.uuid" :dataSource="list" :pagination="pagination" :size="size">
      <a-table-column title="用户" width="40px">
        <template slot-scope="row">
          <div class="user" v-if="row.user">
            <img :src="row.user.headimg" alt="用户头像">
            <div>
              <div>{{ row.user.name }}</div>
              <div>{{row.user.phone}}</div>
            </div>
            <!-- <a-icon class="gender-man" v-if="row.gender==='男'" type="man" />
            <a-icon class="gender-woman" v-else type="woman" /> -->
          </div>
          <div v-else>-</div>
        </template>
      </a-table-column>
      <a-table-column title="app_id" width="100px">
        <template slot-scope="row">
          <div>{{row.app_id}}</div>
        </template>
      </a-table-column>
      <a-table-column title="member_id" width="100px">
        <template slot-scope="row">
          <div>{{row.member_id}}</div>
        </template>
      </a-table-column>
      <a-table-column title="结算账户id" width="100px">
        <template slot-scope="row">
          <div>{{row.settle_account_id}}</div>
        </template>
      </a-table-column>
      <a-table-column title="账户余额" width="60px">
        <template slot-scope="row">
          <div>{{row.acct_balance}}</div>
        </template>
      </a-table-column>
      <a-table-column title="可用余额" width="60px">
        <template slot-scope="row">
          <div>{{row.avl_balance}}</div>
        </template>
      </a-table-column>
      <a-table-column title="身份证号码" width="100px">
        <template slot-scope="row">
          <div>{{row.cert_id}}</div>
        </template>
      </a-table-column>
      <a-table-column title="银行卡户名" width="100px">
        <template slot-scope="row">
          <div>{{row.card_name}}</div>
        </template>
      </a-table-column>      
      <a-table-column title="银行卡号" width="100px">
        <template slot-scope="row">
          <div>{{row.card_id}}</div>
        </template>
      </a-table-column>
      <a-table-column title="银行卡预留手机号码" width="100px">
        <template slot-scope="row">
          <div>{{row.tel_no}}</div>
        </template>
      </a-table-column>      

       <a-table-column title="账户来源" width="100px">
        <template slot-scope="row">
          <div>{{row.user_type | usertypetext}}</div>
        </template>
      </a-table-column>

      <a-table-column title="添加日期" dataIndex="created_at" width="80px">
        <template slot-scope="created_at">
          <span>{{ created_at | onlyDay }}</span>
        </template>
      </a-table-column>
      <a-table-column title="操作" width="100px" v-if="!selectMode">
        <template slot-scope="row">
          <span>
            <a href="javascript:;" @click="handleAgree(row)" v-if="row.status !== 1">审核通过</a>
            <!-- <a href="javascript:;" @click="handleEdit(row)">编辑</a> -->
            <a-divider type="vertical" />
            <a href="javascript:;" @click="handleDelete(row)">删除</a> 
            <a href="javascript:;" @click="handleTixian(row)" style="margin-left:20px;" v-if="user_type==2">手动提现</a>
            
          </span>
        </template>
      </a-table-column>
    </a-table>

    <a-modal
      :title="(editUuid ? '编辑' : '创建') + resourceTypeText"
      style="top: 100px;"
      :visible="isEdit"
      width="1000px"
      @cancel="isEdit = false"
      :footer="null"
    >
      <EditForm v-if="isEdit" :uuid="editUuid" @cancel="isEdit = false" @success="fetchData"></EditForm>
    </a-modal>
  </div>
</template>
<script>
  import moment from 'moment'
  import ISwitch from '@/components/ISwitch/ISwitch'

  const resourceType = 'ada-bank-cards'
  const resourceTypeText = '汇付提现银行卡'

  export default {
    components: {
      ISwitch,
    },
    props: {
      selectMode: {
        type: Boolean
      },
      params: {
        type: Object
      },
      size: {
        type: String,
        default: 'default'
      }
    },
    data() {
      return {
        isEdit: false,
        editUuid: '',
        resourceTypeText,
        userTypeList: [
          {
            text: '前端普通用户',
            key: '1'
          },
          {
            text: '后台提现账户',
            key: '2'
          },
        ],
        user_type:'1',
        card_name: '',
        exportOptions: {
          status: 'all',
          startTime: '',
          endTime: '',
          type: 'normal'
        },
        list: [],
        page: 1,
        per_page: 10,
        total: 0,
        key: '',
        category_id: undefined,
        assetInfo: {

        },
        frame: [
          {
            title: '银行账户类型',
            key: 'bank_acct_type',
            type: 'radio',
            default: 1,
            required: true,
            span: 24,
            options: [{
                title: '对公',
                value: 1
              },
              {
                title: '对私',
                value: 2
              }
            ],
            tips: ''
          },
          {
            title: '银行卡卡号',
            key: 'card_id',
            type: 'input',
            newline: true,
            required: true,
          },
          {
            title: '银行卡对应的户名',
            key: 'card_name',
            type: 'input',
            newline: true,
            required: true,
          },
          {
            title: '银行卡预留手机号',
            key: 'tel_no',
            type: 'input',
            newline: true,
            required: true,
          },
          {
            title: '身份证号码',
            key: 'cert_id',
            type: 'input',
            newline: true,
            required: true,
            where: {
              bank_acct_type: 2
            }
          },
          {
            title: '企业名称',
            key: 'name',
            type: 'input',
            newline: true,
            required: true,
            where: {
              bank_acct_type: 1
            }
          },
          {
            title: '统一社会信用码',
            key: 'social_credit_code',
            type: 'input',
            newline: true,
            required: true,
            where: {
              bank_acct_type: 1
            }
          },
          {
            title: '统一社会信用证有效期',
            key: 'social_credit_code_expires',
            type: 'input',
            newline: true,
            required: true,
            where: {
              bank_acct_type: 1
            }
          },
          {
            title: '经营范围',
            key: 'business_scope',
            type: 'input',
            newline: true,
            required: true,
            where: {
              bank_acct_type: 1
            }
          },
          {
            title: '法人姓名',
            key: 'legal_person',
            type: 'input',
            newline: true,
            required: true,
            where: {
              bank_acct_type: 1
            }
          },
          {
            title: '法人身份证号码',
            key: 'legal_cert_id',
            type: 'input',
            newline: true,
            required: true,
            where: {
              bank_acct_type: 1
            }
          },
          {
            title: '法人身份证有效期',
            key: 'legal_cert_id_expires',
            type: 'input',
            newline: true,
            required: true,
            where: {
              bank_acct_type: 1
            }
          },
          {
            title: '法人手机号',
            key: 'legal_mp',
            type: 'input',
            newline: true,
            required: true,
            where: {
              bank_acct_type: 1
            }
          },
          {
            title: '企业地址',
            key: 'address',
            type: 'input',
            newline: true,
            required: true,
            where: {
              bank_acct_type: 1
            }
          },
          {
            title: '银行编码',
            key: 'bank_code',
            type: 'input',
            newline: true,
            required: true,
            where: {
              bank_acct_type: 1
            }
          },
          {
            title: '开户银行所在省份编码',
            key: 'prov_code',
            type: 'input',
            newline: true,
            required: true,
            where: {
              bank_acct_type: 1
            }
          },
          {
            title: '开户银行所在地区编码',
            key: 'area_code',
            type: 'input',
            newline: true,
            required: true,
            where: {
              bank_acct_type: 1
            }
          },
        ],
        frame2: [
          {
            title: '操作类型',
            key: 'do_type',
            type: 'radio',
            default: 1,
            required: true,
            span: 24,
            options: [{
                title: '余额支付&提现',
                value: 1
              },
              {
                title: '余额支付',
                value: 2
              },
              {
                title: '提现',
                value: 3
              }
            ],
            tips: ''
          },
          {
            title: '取现类型',
            key: 'cash_type',
            type: 'radio',
            default: 'DM',
            required: true,
            span: 24,
            options: [{
                title: 'T+1取现',
                value: 'T1'
              },
              {
                title: 'DM-可用额度取现',
                value: 'DM'
              }
            ],
            tips: ''
          },
          {
            title: '请输入金额',
            key: 'cash_amt',
            type: 'number',
            tips: '单位：元',
            newline: true,
            required: true,
          }
        ],        
      }
    },
    watch: {
      params() {
        this.refreshData()
      },
      category_id() {
        this.refreshData()
      },
      user_type() {
        this.refreshData()
      }
    },
    computed: {
      pagination() {
        return {
          defaultPageSize: this.per_page,
          defaultCurrent: this.page,
          total: this.total,
          onChange: (e) => {
            this.page = e.current
            this.per_page = e.pageSize
            this.fetchData()
          }
        }
      }
    },
    filters: {
      typetext(value){
        if(value == 1){
          return '银行卡';
        }else if(value == 2){
          return '支付宝';
        }else{
          return '未知';
        }
      },
      usertypetext(value){
        if(value == 1){
          return '前端普通用户';
        }else{
          return '后台提现账户';
        }
      }
    },
    created() {
      this.fetchData()
      // this.getCategoryList()
    },
    methods: {
      handleAgree(row) {
        this.$confirm({
          title: '提示',
          content: `确认同意吗?`,
          onOk: () => {
            this.$http.patch(`/${resourceType}/${row.uuid}`, {
              type: 'agree'
            }).then(res => {
              this.fetchData()
              this.fetchTotalData()
            })
          }
        })
      },
      handleDelete(row) {
        this.$confirm({
          title: '提示',
          content: `确认删除吗？`,
          onOk: () => {
            this.$http.delete(`/${resourceType}/${row.id}`).then(res => {
              this.fetchData()
            })
          }
        })
      },
      updateItem(row, data) {
        return this.$http.patch(`/${resourceType}/${row.uuid}`, {
          type: 'update',
          attributes: {
            ...data
          }
        })
      },
      recommendStatusChange(e, row) {
        this.updateItem(row, {
          is_recommend: e
        }).then(res => {
          this.fetchData()
        })
      },
      listedStatusChange(e, row) {
        this.updateItem(row, {
          status: e
        }).then(res => {
          this.fetchData()
        })
      },
      handleEditListWeight(row) {
        this.$globalForm.show({
          title: '编辑排序',
          items: [{
            type: 'number',
            key: 'list_weight',
            title: '排序',
            default: row.list_weight,
            required: true
          }, ],
          success: (data) => {
            this.updateItem(row, {
              ...data
            }).then(res => {
              this.$message.success('保存成功');
              this.$globalForm.hide()
              this.fetchData()
            })
          }
        })
      },
      handleCreate() {
        this.$iForm.show({
          title: '新增后台提现账户',
          frame: this.frame,
          success: (data) => {
            this.$http.post(`/ada-bank-card/add`, {
              ...data,
            }).then(res => {
              this.fetchData()
              this.$iForm.hide()
            })
          },
        })
      },
      handleTixian(row) {
        this.$iForm.show({
          title: '提现户名：' + row.card_name + ' ，提现卡号：' + row.card_id,
          frame: this.frame2,
          success: (data) => {
            this.$http.post(`/ada-bank-card/drawCash`, {
			  id: row.id,
              member_id: row.member_id,
			  user_id: row.user_id,
              ...data,
            }).then(res => {
              this.fetchData()
              this.$iForm.hide()
            })
          },
        })
      },      
      handleEdit(row) {
        this.editUuid = row.uuid
        this.isEdit = true
      },
      refreshData() {
        this.page = 1
        this.key = ''
        this.fetchData()
      },
      onSearch(e) {
		  this.page = 1
        this.fetchData()
        // clearTimeout(this.timeout)
        // this.timeout = setTimeout(() => {
        //   this.fetchData()
        // }, 300)
      },
      fetchData() {
        this.isEdit = false
        this.$http.get(`/${resourceType}`, {
          params: {
            ...this.params,
            page: this.page,
            per_page: this.per_page,
            key: this.key,
            user_type: this.user_type,
          }
        }).then(res => {
          this.list = res.data.list
          this.total = res.data.item_total
        }).finally(() => {
          this.loading = false
        })

      },
      getCategoryList() {
        this.$api.emit('category.list', 'article').then(res => {
          this.category = res.data.list
        })
      },
    }
  }
</script>
<style lang="scss" scoped>
  .value-font {
    font-weight: 500;
    font-size: 110%;
  }
  .content-head {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
  }

  .sort-keys {
    display: flex;
    align-items: center;

    .tab-list {
      display: flex;
      margin-top: 6px;
      border-bottom: 1px solid #e1e1e1;
      margin-right: 50px;

      .item {
        margin-right: 30px;
        padding: 0px 10px 4px 10px;
        cursor: pointer;
        font-weight: 500;

        &.actived {
          color: #1890FF;
          border-bottom: 3px solid #1890FF;
        }

        &:hover {
          color: #1890FF;
        }
      }
    }

  }

  .table {
    .thumb {
      height: 60px;
      border-radius: 6px;
    }

    .content-image {
      height: 60px;
      width: 60px;
      margin-right: 10px;
      margin-bottom: 10px;
      object-fit: cover;
      border-radius: 10px;
      cursor: pointer;

      &:hover {
        opacity: 0.8;
      }
    }

    .user {
      display: flex;
      // flex-wrap: wrap;
      align-items: center;
      img {
        width: 26px;
        height: 26px;
        margin-right: 6px;
        border-radius: 50%;
        overflow: hidden;
      }
      .gender-man, .gender-woman {
        margin-left: 5px;
        font-size: 12px;
      }
      .gender-man {
        color: #409EFF;
      }
      .gender-woman {
        color: #F56C6C;
      }
    }
  }
</style>
